package com.alfredo.android.appcaja;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.alfredo.android.appcaja.adapters.OrdenesAdapter;
import com.alfredo.android.appcaja.constants.Keys;
import com.alfredo.android.appcaja.dataprovider.DataProvider;
import com.alfredo.android.appcaja.model.Orden;

public class OrdenesVerificacionActivity extends Activity 
{
	private OrdenesAdapter ordenesAdapter;
	private ProgressBar pb_ordenes;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ordenes_verificacion);
		
		ordenesAdapter = new OrdenesAdapter(this);
		
		pb_ordenes = (ProgressBar) findViewById(R.id.pb_ordenes);
		
		ListView list_ordenes = (ListView) findViewById(R.id.list_ordenes);
		list_ordenes.setAdapter(ordenesAdapter);
		list_ordenes.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
			{
				Orden orden = (Orden) parent.getItemAtPosition(position);
				Intent intent = new Intent(OrdenesVerificacionActivity.this, DetalleOrdenActivity.class);
				intent.putExtra(Keys.ORDEN, orden);
				startActivity(intent);
			}
		});
		
		String estadoVerificacion = null;
		String cgi = null;
		String nroExpediente = null;
		String causanteBeneficiario = null;
		
		if(getIntent().getExtras() != null)
		{
			estadoVerificacion = getIntent().getExtras().getString(Keys.ESTADO_VERIFICACION);
			cgi = getIntent().getExtras().getString(Keys.CGI);
			nroExpediente = getIntent().getExtras().getString(Keys.NUMERO_EXPEDIENTE);
			causanteBeneficiario = getIntent().getExtras().getString(Keys.CAUSANTE_BENEFICIARIO);
		}
		
		new OrdenesTask(estadoVerificacion, cgi, nroExpediente, causanteBeneficiario).execute();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		getMenuInflater().inflate(R.menu.ordenes_verificacion, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		if(item.getItemId() == R.id.action_nuevo)
		{
			Intent intent = new Intent(this, DetalleOrdenActivity.class);
			startActivity(intent);
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	private class OrdenesTask extends AsyncTask<Void, Integer, ArrayList<Orden>>
	{
		private String estadoVerificacion;
		private String cgi;
		private String nroExpediente;
		private String causanteBeneficiario;
		
		public OrdenesTask(String estadoVerificacion, String cgi, String nroExpediente, String causanteBeneficiario)
		{
			this.estadoVerificacion = estadoVerificacion;
			this.cgi = cgi;
			this.nroExpediente = nroExpediente;
			this.causanteBeneficiario = causanteBeneficiario;
		}
		
		@Override
		protected ArrayList<Orden> doInBackground(Void... params) 
		{
			return DataProvider.getInstance().getOrdenes(estadoVerificacion, cgi, nroExpediente, causanteBeneficiario);
		}
		
		@Override
		protected void onPostExecute(ArrayList<Orden> ordenes) 
		{
			for(Orden orden: ordenes)
			{
				ordenesAdapter.add(orden);
			}
			
			pb_ordenes.setVisibility(View.GONE);
		}
	}
}
